﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Drawing;
using Truextend.Training.Presenter.ViewInterfaces;
using System.Collections.Generic;
using Truextend.Training.Model.Entities;
using Truextend.Training.Presenter;


namespace Truextend.Training.CoursesCalendar
{
    public partial class CoursesCalendarUserControl : UserControl, ICourseCalendarView
    {
        CourseCalendarPresenter presenter;

        protected void Page_Load(object sender, EventArgs e)
        {   
            spCalendar.EnableViewState = true;
            spCalendar.DownLevelRendering = true;
            spCalendar.EnableV4Rendering = true;/*
            spCalendar.EnableTheming = true;
            spCalendar.Width = Unit.Percentage(100);*/
            
            
            //spCalendar.Enabled = false;
            //spCalendar.ForeColor = Color.Red;
            //spCalendar.ViewType = "Week";
            presenter = new CourseCalendarPresenter(this);
            presenter.InitView();
            
        }


        private SPCalendarItemCollection GetCalendarItems(IList<CourseInstance> list)
        {

            SPCalendarItemCollection items = new SPCalendarItemCollection();

            foreach (CourseInstance courseInstance in list)
            {
                foreach(ScheduleCourse scheduleCourse in courseInstance.ScheduleCourses)
                {
                    SPCalendarItem item = new SPCalendarItem();

                    item.StartDate = scheduleCourse.StartTime;
                    item.EndDate = scheduleCourse.EndTime;
                    item.hasEndDate = true;
                    item.Title = courseInstance.Course.Name;
                    //item.DisplayFormUrl = "/News";
                    item.Location = "USA";
                    item.Description = courseInstance.Course.Category;
                    item.IsAllDayEvent = true;
                    item.IsRecurrence = true;
                    item.CalendarType = Convert.ToInt32(SPCalendarType.Gregorian);

                    items.Add(item);
                }                
            }
            
            return items;
        }

        public IList<CourseInstance> CourseInstances
        {
            set
            {
                spCalendar.DataSource = GetCalendarItems(value);
                spCalendar.DataBind();
            }
        }
    }
}
